package com.luckberry.murder.controller.api;

import com.alibaba.fastjson.JSON;
import com.luckberry.murder.entity.Script;
import com.luckberry.murder.service.ScriptService;
import com.luckberry.murder.util.JSONUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Controller
@RequestMapping("/api")
public class ApiScriptController {

    @Resource
    private ScriptService service;

    @ResponseBody
    @RequestMapping(value = "/scripts", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public String getScripts() {
        return JSON.toJSONString(service.getScripts());
    }

    @ResponseBody
    @RequestMapping(value = "/script/{sid}", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public String getScript(@PathVariable("sid") int sid) {
        return JSON.toJSONString(service.getScript(sid));
    }

    @ResponseBody
    @RequestMapping(value = "/script", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)
    public String addScript(@RequestBody String json) {
        return JSONUtil.result(service.addScript(JSON.parseObject(json, Script.class)));
    }

    @ResponseBody
    @RequestMapping(value = "/script/{sid}", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.DELETE)
    public String deleteScript(@PathVariable("sid") int sid) {
        return JSONUtil.result(service.deleteScript(sid));
    }

    @ResponseBody
    @RequestMapping(value = "/script", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.PUT)
    public String editScript(@RequestBody String json) {
        return JSONUtil.result(service.editScript(JSON.parseObject(json, Script.class)));
    }
}
